<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\api\v1\user;

use app\dao\message\service\StoreServiceRecordDao;
use app\Request;
use app\services\message\service\StoreServiceRecordServices;
use app\services\message\SystemMessageServices;
use think\facade\Db;



class SystemMessage
{
    protected $services = NUll;

    /**
     * SystemMessage constructor.
     * @param SystemMessageServices $services
     */
    public function __construct(SystemMessageServices $services)
    {
        $this->services = $services;
    }

	/**
	 * 获取用户消息 站内信+客服消息
	 * @param Request $request
	 * @param StoreServiceRecordServices $services
	 * @return \think\Response
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function message(Request $request,StoreServiceRecordServices $services)
	{
		$uid = (int)$request->uid();
		$where['is_del'] = 0;
		$where['uid'] = $uid;
		$list = $this->services->getMessageList($where, '*', 0, 1);
		$list = $list[0] ?? [];
		if ($list) {
			$list['add_time'] = time_tran($list['add_time']);
			$list['message_num'] = $this->services->count(['uid' => $uid, 'look' => 0, 'is_del' => 0]);
		}

		$service = $services->getServiceList($uid);
		return app('json')->successful(['system' => $list, 'service' => $service]);
	}
    public function  sticky(Request $request)
    {
        [$id] = $request->postMore([ ['id', '']],true);

       $sticky = DB::name('store_service_record')->where(['id'=>$id])->value('sticky');
       if ($sticky>=1){
           DB::name('store_service_record')->where(['id'=>$id])->update(['sticky'=>0]);
       }else{
           DB::name('store_service_record')->where(['id'=>$id])->update(['sticky'=>1]);
       }
        return app('json')->success('ok');

    }


    /**
     * 站内信列表
     * @param Request $request
     * @return mixed
     */
    public function message_list(Request $request)
    {
        $uid = (int)$request->uid();
        $page = (int)$request->get('page', 1);
        $limit = (int)$request->get('limit', 10);
        return app('json')->successful($this->services->getSystemMessageList($uid,$page, $limit));
    }

    /**
     * 站内信详情
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function detail(Request $request, $id)
    {
        if (!$id) {
            app('json')->fail('缺少参数');
        }
        $uid = (int)$request->uid();
        return app('json')->successful($this->services->getInfo((int)$id, $uid));
    }
}
